# -*- coding:utf-8 -*-
import urllib
import time
import os

sinaAPIKey = '1330786948'
sinaAPISecret = '94fa907c07c9d6915927f48149f431d8'


def runcrawler(thepath):
    """
    crawler per second?
    check the same?
    """
    for i in range(750):
        _crawler_public_func(thepath)
        time.sleep(1)


def _crawler_public_func(thepath):
    """
    @param thepath 
    crawler the sina by access the sinaAPI open status
    10000次/小时
    3600s
    call the crawler per second
    http://api.t.sina.com.cn/statuses/public_timeline.json?source=1330786948&count=200
    """
    theurl = "http://api.t.sina.com.cn/statuses/public_timeline.json?count=200&source="+sinaAPIKey
    thesocket = urllib.urlopen(theurl)
    thestr = thesocket.read()

    fname = time.strftime(thepath+"/"+"%Y_%m_%d_%H_%M_%S",time.localtime(time.time()))
    f = open(fname,"w")
    print>>f,thestr
    f.close()

if __name__ == "__main__":
    thepath = "./aprilthird"
    try:
        os.mkdir(thepath)
    except:
        print thepath +" is already made"
    runcrawler(thepath)
